//
//  ViewController.m
//  09-Homework
//
//  Created by dllo on 16/7/26.
//  Copyright © 2016年 LST. All rights reserved.
//

#import "ViewController.h"
#import "Pro.h"
#import "City.h"
#import "Zone.h"
#import "CityViewController.h"
#import "ProViewController.h"
#define kTableViewCellReuse @"reuseIdentifier"
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *Proarr;


@end

@implementation ViewController
- (NSMutableArray *)Proarr {
    if (!_Proarr) {
        self.Proarr = [NSMutableArray array];
    }
    return _Proarr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBarHidden = NO;
    self.title = @"中国";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(clickLeftBtn:)];
    [self creatNSString];
    [self creatTableView];
   
    
}
- (void)creatNSString {
    NSString *path = @"/Users/dllo/DLS160710/UI/HomeWork/09-Homework/09-Homework/area.txt";
    self.Proarr = [NSMutableArray array];
    NSString *areaString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSArray *strArr = [areaString componentsSeparatedByString:@"\n"];
    for (NSString *area in strArr) {
        if (![area hasPrefix:@" "]) {
            Pro *pro = [Pro zoneWithName:area];
            [_Proarr addObject:pro];
        } else if ([area hasPrefix:@"  "] && ![area hasPrefix:@"    "]) {
            Pro *pro = [_Proarr lastObject];
            City *city = [City zoneWithName:area];
            [pro.array addObject:city];
        } else {
            City *city = [[[_Proarr lastObject] array] lastObject];
            Zone *zone = [Zone zoneWithName:area];
            [city.array addObject:zone];
           
        }
    }
    NSLog(@"%@", _Proarr);
}

- (void)creatTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _Proarr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _Proarr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    Pro *pro = _Proarr[indexPath.row];
    cell.textLabel.text = pro.name;

    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    CityViewController *city = [[CityViewController alloc] init];
    city.proModel = _Proarr[indexPath.row];
    [self.navigationController pushViewController:city animated:YES];
}
- (void)clickLeftBtn:(UIBarButtonItem *)leftBtn {
    ProViewController *proVC = [[ProViewController alloc] init];
    [self.navigationController pushViewController:proVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
